//
//  JXTabBar.swift
//  sinaWeibo
//
//  Created by jixiao on 15/12/27.
//  Copyright © 2015年 heimacast. All rights reserved.
//

import UIKit

class JXTabBar: UITabBar {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(compuse)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //定义一个可选的属性,类型是闭包
    var didClickBack:(()->())?
    
    
    
    override func layoutSubviews() {
        
        //如果没有设置super方法调用,就是透明的
        super.layoutSubviews()
        
        
        compuse.center.x=bounds.size.width*0.5
        
        compuse.center.y=bounds.size.height*0.5
        
        //遍历子tabbarItem,添加一个加号
        
        var index:CGFloat = 0
        
        let kWidth = bounds.size.width * 0.2
        
        for value in subviews{
            
            //如果是UITabBarButton类型,就设置他的x为一定的值
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.origin.x=index * kWidth
                value.frame.size.width=kWidth
                
                
                index++
                
                if index == 2 {
                    
                    index++
                    
                }
                
                
            }
            
        }
        
        
    }
    
    private lazy var compuse:UIButton={
    
        let btn=UIButton()
        
        //设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        //自动调整大小
        btn.sizeToFit()
        //设置点击事件
        btn.addTarget(self, action: "didClickAddBtn", forControlEvents: .TouchUpInside)
        
        
        return btn
    }()
    
    //凡是依赖于运行时的方法一运行就报错,因为swift为了更加快捷是静态语言
    //需要添加一个@objc
    @objc private func didClickAddBtn(){
        
        
        
        didClickBack?()
        
        
    }
    

}
